//
//  DateUtil.m
//  EliveClient
//
//  Created by Pierce on 2017/8/9.
//  Copyright © 2017年 Pierce. All rights reserved.
//

#import "DateUtil.h"

@implementation DateUtil

//DateFormatterStyle的枚举转成具体时间格式，此处可依据需求自己修改
+(NSString *)getFormatByStyleEnum:(DateFormatterStyle)format{
    switch (format) {
        case DateStyleDefault_CUSTOM:
            return FORMAT_CUSTOM;
            break;
        case DateStyleDefault_YMD:
            return @"YYYY-MM-dd";
            break;
        case DateStyleDefault_YMDHMS:
            return @"YYYY-MM-dd HH:mm:ss";
            break;
        case DateStyleDoublePoint:
            return @"YYYY:MM:dd";
            break;
        case DateStyleInclined:
            return @"YYYY/MM/dd";//DateStyleDefault_HMS
            break;
        case DateStyleDefault_HMS:
            return @"HH:mm:ss";
            break;
        case DateStyleDefault_MS:
            return @"mm:ss";
            break;
        case DateStyleDefault_HM:
            return @"HH:mm";
            break;
        default:
            return FORMAT_DEFULT;
            break;
    }
}


+(NSString *)getNowDateByFormatStyle:(DateFormatterStyle)formatStyle{
    NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
    NSString * formatString = [self getFormatByStyleEnum:formatStyle];
    [formatter setDateFormat:formatString];
    return  [formatter stringFromDate:[NSDate date]];
}


+(NSDate *)getNowDate{
    return [NSDate date];
}

+(NSString *)stringFromDate:(NSDate *)date Format:(DateFormatterStyle)formatStyle{
    NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
    NSString * formatString = [self getFormatByStyleEnum:formatStyle];
    [formatter setDateFormat:formatString];
    return  [formatter stringFromDate:date];
}

+(NSDate *)dateFromString:(NSString *)dateString Format:(DateFormatterStyle)formatStyle{
    NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
    NSString * formatString = [self getFormatByStyleEnum:formatStyle];
    [formatter setDateFormat:formatString];
    return  [formatter dateFromString:dateString];
    
}

+ (NSString *)dateStringWithLong:(long)value Format:(DateFormatterStyle)formatStyle{
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:value/1000];
    return [self stringFromDate:date Format:formatStyle];
    
}


// 获取当前是星期几
+ (NSString *)getNowWeekday {
    
    return  [self getNowWeekdayByDate:[NSDate date]];
}

+ (NSString *)getNowWeekdayByDate:(NSDate *)date{
    
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    NSInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday |
    NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    // 话说在真机上需要设置区域，才能正确获取本地日期，天朝代码:zh_CN
    calendar.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
    comps = [calendar components:unitFlags fromDate:date];
    NSInteger  integer = [comps weekday];
    switch (integer) {
        case 1:
            return @"星期天";
            break;
        case 2:
            return @"星期一";
            break;
        case 3:
            return @"星期二";
            break;
        case 4:
            return @"星期三";
            break;
        case 5:
            return @"星期四";
            break;
        case 6:
            return @"星期五";
            break;
        case 7:
            return @"星期六";
            break;
            
        default:
            return @"星期获取失败";
            break;
    }
    
}

//获取一周时间 数组
+ (NSMutableArray *)getCurrentWeeksWithFirstDiff:(NSInteger)first lastDiff:(NSInteger)last{
    NSMutableArray *eightArr = [[NSMutableArray alloc] init];
    for (NSInteger i = first; i < last + 1; i ++) {
        //从现在开始的24小时
        NSTimeInterval secondsPerDay = i * 24*60*60;
        NSDate *curDate = [NSDate dateWithTimeIntervalSinceNow:secondsPerDay];
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];         [dateFormatter setDateFormat:@"M月d日"];
        NSString *dateStr = [dateFormatter stringFromDate:curDate];//几月几号
        //        NSString *dateStr = @"5月31日";
        NSDateFormatter *weekFormatter = [[NSDateFormatter alloc] init];
        [weekFormatter setDateFormat:@"EEEE"];//星期几 @"HH:mm 'on' EEEE MMMM d"];
        NSString *weekStr = [weekFormatter stringFromDate:curDate];
        //组合时间
        NSString *strTime = [NSString stringWithFormat:@"%@(%@)",dateStr,weekStr];          [eightArr addObject:strTime];
    }
    return eightArr;
}

+ (NSCalendar*)getCalendar {
    //初始化日历类，并设置日历类的格式是阳历 若想设置中国日历 设置为NSChineseCalendar
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    //设置每周的第一天从星期几开始：1是周日，2是周一
    [calendar setFirstWeekday:1];
    //设置每个月或者每年的第一周必须包含的最少天数  设置为1 就是第一周至少要有一天
    [calendar setMinimumDaysInFirstWeek:1];
    //设置时区，不设置时区获取月的第一天和星期的第一天的时候可能会提前一天。
    [calendar setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:3600*8]];
    return calendar;
}

+ (NSDate*)getFirstDateOfWeek:(NSDate*)date {
    NSTimeInterval interval;
    NSDate *firstDayOfWeek;
    //如果firstDayOfWeek和interval可计算，下边这个方法会返回YES，
    //并且由firstDayOfWeek可得到date所在的设置的时间段（NSWeekCalendarUnit）里的第一天
    if ([[DateUtil getCalendar] rangeOfUnit: NSCalendarUnitWeekOfYear startDate:&firstDayOfWeek interval:&interval forDate:date]) {
        //NSLog(@"firstDayOfWeek = %@",firstDayOfWeek);
    }
    return firstDayOfWeek;
}

+ (NSInteger)getNowDateIndexDayInWeek:(NSDate *)date {
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    NSInteger unitFlags =NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    comps = [calendar components:unitFlags fromDate:date];
    return comps.weekday;
}

+ (NSString *)getDateWithTimeInterval:(NSString *)TimeIntervalStr type:(NSString *)formatterType{
    NSTimeInterval time=[TimeIntervalStr doubleValue];//因为时差问题要加8小时 == 28800 sec
    NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:time];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //设定时间格式,这里可以设置成自己需要的格式
    [dateFormatter setDateFormat:formatterType];
    
    NSString *currentDateStr = [dateFormatter stringFromDate: detaildate];
    return currentDateStr;
}

// 返回时间戳
+ (NSString *)getNowDateWithTime{
    NSDate *nowDate = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyyMMddHHmmss"];
    NSString *timeSp = [NSString stringWithFormat:@"%ld",(long)[nowDate timeIntervalSince1970]];
    return timeSp;
    
}

// 返回时间戳（精确）
+ (NSString *)getNowDateWithExactTime{
    UInt64 recordTime = [[NSDate date] timeIntervalSince1970]*1000;
    return [NSString stringWithFormat:@"%llu",recordTime];
}

//计算时间差
+ (NSTimeInterval)pleaseInsertStarTime:(NSString *)starTime andInsertEndTime:(NSString *)endTime format:(NSString *)format{
    NSDateFormatter* formater = [[NSDateFormatter alloc] init];
    [formater setDateFormat:format];//根据自己的需求定义格式
    NSDate* startDate = [formater dateFromString:starTime];
    NSDate* endDate = [formater dateFromString:endTime];
    NSTimeInterval time = [endDate timeIntervalSinceDate:startDate];
    return time;
}

+(NSInteger)timeSwitchTimestamp:(NSString *)formatTime andFormatter:(NSString *)format{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:format]; //(@"YYYY-MM-dd hh:mm:ss") ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    [formatter setTimeZone:timeZone];
    NSDate* date = [formatter dateFromString:formatTime]; //------------将字符串按formatter转成nsdate
    NSInteger timeSp = [[NSNumber numberWithDouble:[date timeIntervalSince1970]] integerValue];
    return timeSp;
    
}

+(NSDate*)getTodayDate{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDate *now = [NSDate date];
    NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:now];
    NSDate *startDate = [calendar dateFromComponents:components];
    //后一天的0点
  //  NSDate *endDate = [calendar dateByAddingUnit:NSCalendarUnitDay value:1 toDate:startDate options:0];
    return startDate;
}



@end
